export default (lat1, lng1, lat2, lng2) => {
	const PI = Math.PI
	const EARTH_RADIUS = 6378137.0

	let f = ((lat1 + lat2) / 2) * PI / 180.0;
	let g = ((lat1 - lat2) / 2) * PI / 180.0;
	let l = ((lng1 - lng2) / 2) * PI / 180.0;

	let sg = Math.sin(g)
	let sl = Math.sin(l)
	let sf = Math.sin(f)

	let s, c, w, r, d, h1, h2
	let a = EARTH_RADIUS
	let fl = 1 / 298.257

	sg = sg * sg
	sl = sl * sl
	sf = sf * sf

	s = sg * (1 - sl) + (1 - sf) * sl
	c = (1 - sg) * (1 - sl) + sf * sl

	w = Math.atan(Math.sqrt(s / c))
	r = Math.sqrt(s * c) / w
	d = 2 * w * a
	h1 = (3 * r - 1) / 2 / c
	h2 = (3 * r + 1) / 2 / s
	// console.log(d * (1 + fl * (h1 * sf * (1 - sg) - h2 * (1 - sf) * sg)));
	return d * (1 + fl * (h1 * sf * (1 - sg) - h2 * (1 - sf) * sg))
}
